package accounting_system;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class PersonCardFrame extends JFrame{
    private Person person;
    
    private boolean edit = false;
    
    public PersonCardFrame(Person p){
        super();
        
        //if (person.getSurName().isEmpty() || person.getFirstName().isEmpty()) setTitle("пустой");
        //else setTitle(person.getSurName() + " " + person.getFirstName());
        person = p;
        
        //Image photo;
        PhotoPanel photo = new PhotoPanel();
        JLabel surNameLabel = new JLabel("Фамилия");
        JTextField surNameField = new JTextField(person.getSurName());
        JLabel firstNameLabel = new JLabel("Имя");
        JTextField firstNameField = new JTextField(person.getFirstName());
        JLabel secondNameLabel = new JLabel("Отчество");
        JTextField secondNameField = new JTextField(person.getSecondName());
        JLabel birthDateLabel = new JLabel("Дата рождения");
        DatePanel birthDateField = new DatePanel(person.getBirthDate());
        JLabel workStartDateLabel = new JLabel("Дата приема");
        DatePanel workStartDateField = new DatePanel(person.getWorkStartDate());
        JLabel workFinishDateLabel = new JLabel("Дата увольнения");
        DatePanel workFinishDateField = new DatePanel(person.getWorkFinishDate());
        JLabel functionLabel = new JLabel("Должность");
        JTextField functionField = new JTextField(person.getFunction());
        JLabel levelLabel = new JLabel("Уровень");
        JComboBox levelField = new JComboBox();
        JButton editButton = new JButton("Редактировать");
        JButton okButton = new JButton("Сохранить");
        JButton cancelButton = new JButton("Закрыть");
        JPanel panel = new JPanel();
        
        //surNameField.setColumns(20);
//        firstNameField.setColumns(20);
//        secondNameField.setColumns(20);
        
        
        
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5), BorderFactory.createEtchedBorder(1)));
        
        
        gbc.insets.set(5, 5, 0, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridx = 0; //GridBagConstraints.RELATIVE;
        gbc.gridy = 0;
        gbc.gridheight = 4;
        panel.add(photo, gbc);
        
        //gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 1;
        gbc.gridheight = 1;
        panel.add(surNameLabel, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridwidth = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        //gbc.gridwidth = 2;
        panel.add(surNameField, gbc);
        
        gbc.gridx = 1;//GridBagConstraints.REMAINDER;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(firstNameLabel, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridwidth = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(firstNameField, gbc);
        
        gbc.gridx = 1; //= GridBagConstraints.REMAINDER;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        panel.add(secondNameLabel, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridwidth = GridBagConstraints.LINE_END;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(secondNameField, gbc);
        
        gbc.gridx = 1;  //GridBagConstraints.REMAINDER;
        gbc.gridy = 3;
        //gbc.gridwidth = 1;
        panel.add(birthDateLabel, gbc);
        
        gbc.gridx = 2; //GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(birthDateField, gbc);
        
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        panel.add(new HorizontalLine(), gbc);
        
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 5;
        panel.add(workStartDateLabel, gbc);
        
        gbc.gridx = 1;
        panel.add(workStartDateField, gbc);
        
//        gbc.gridx = 2;
//        panel.add(new JPanel(), gbc);
        gbc.gridwidth = 2;
        gbc.gridx = 3;
        gbc.insets.set(0, 30, 0, 0);
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(workFinishDateLabel, gbc);
        
        //gbc.gridx = 3;
        gbc.insets.set(5, 5, 0, 5);
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridwidth = 2;
        //gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(workFinishDateField, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(functionLabel, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 4;
        panel.add(functionField, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        //gbc.fill = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridwidth = 1;
        panel.add(levelLabel, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        //gbc.anchor = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        panel.add(levelField, gbc);
        
        gbc.gridx = GridBagConstraints.REMAINDER;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(new HorizontalLine(), gbc);
        gbc.insets.set(1, 5, 0, 5);
        panel.add(new HorizontalLine(), gbc);
        
        gbc.insets.set(20, 5, 5, 5);
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;  //GridBagConstraints.RELATIVE;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        editButton.setEnabled(edit);
        panel.add(editButton, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridwidth = 1;
        panel.add(new JPanel(), gbc);
        
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.LINE_END;
        okButton.setEnabled(edit);
        panel.add(okButton, gbc);
        
        gbc.gridwidth = 2;
        panel.add(cancelButton, gbc);
                
        add(panel);        
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
//    public void setPersonData(Person p){
//        person = p;
//    }
//    
//    public void removePersonData(){
//        person = null;
//    }
    
    public PersonCardFrame getPersonCard(){
        return this;
    }
    
    public void setEditable(boolean edit){
        this.edit = edit;
    }
    
    public boolean isEditable(){
        return edit;
    }
//    @Override
//    public void paint(Graphics g){
//        
//    }
    
}
class PhotoPanel extends JPanel{
    
    public PhotoPanel(String imageFile){
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setPreferredSize(new Dimension(75, 100));
        setSize(getPreferredSize());
    }
    
    public PhotoPanel(){
        this(null);
    }
    
   
}

class HorizontalLine extends JComponent{
    
    public HorizontalLine(){
        
    }
    
    @Override
    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        g.drawLine(0, 0, getWidth(), 0);
    }
}